Syntactic sugar

Syntactic sugar is a computer science term that refers to syntax within a programming language that is designed to make things easier to read or to express.

It makes the language "sweeter" for humans to use: things can be expressed more clearly, more concisely, or in an alternative style that some may prefer.

Specifically, a construct in a language is called syntactic sugar if it can be removed from the language without any effect on what the language can do: functionality and expressive power will remain the same. All applications of the construct can be systematically replaced with equivalents that do not use it. For instance, in imperative programming languages, for loops can be systematically replaced with while loops, which in turn can be systematically replaced with gotos.

More generally, the term is used to characterize syntax as being designed for ease of expression. For instance, in C#, the property construct may be called syntactic sugar: it is roughly, but not exactly equivalent to a getter-setter pair of functions.

Contents

Origins

The term syntactic sugar was coined by Peter J. Landin in 1964 to describe the surface syntax of A Programming Language (APL) which was defined semantically in terms of the applicative expressions of lambda calculus.[1]

Later programming languages, such as ML and Scheme, extended the term to refer to syntax within a language which could be defined in terms of a language core of essential constructs; the convenient, higher-level features could be "desugared" and decomposed into that subset. This is, in fact, the usual mathematical practice of building up from primitives.

Criticism

Some programmers feel that these syntax usability features are either unimportant or outright frivolous. For example, Alan Perlis once quipped, in a reference to bracket-delimited languages, that "syntactic sugar causes cancer of the semicolon". (See Epigrams on Programming.)

Derivative terms

Syntactic salt

The metaphor has been extended by coining the term syntactic salt, which indicates a feature designed to make it harder to write bad code. Specifically, syntactic salt is a hoop programmers must jump through just to prove that they know what's going on, rather than to express a program action. For example, Java will not allow you to declare a variable as an int and then assign it a float or double value, while C and C++ will automatically truncate any floats assigned to an int.

Syntactic saccharin

Another extension is syntactic saccharin, meaning gratuitous syntax that does not actually make programming easier.[2]

Notes and references

  1. ^ Peter J. Landin, "The mechanical evaluation of expressions", Computer Journal 6:4: 308-320 (1964) doi:10.1093/comjnl/6.4.308
  2. ^ The Jargon File v4.4.7: "syntactic sugar"

This article was originally based on material from the Free On-line Dictionary of Computing, which is licensed under the GFDL.